package com.example.shoppingmall.cotroller;

import com.alibaba.fastjson.JSONArray;
import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Module;
import com.example.shoppingmall.pojo.Page;
import com.example.shoppingmall.pojo.Role;
import com.example.shoppingmall.pojo.RoleJurisdiction;
import com.example.shoppingmall.service.ModuleService;
import com.example.shoppingmall.service.RoleJurisdictionService;
import com.example.shoppingmall.service.RoleService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleJurisdictionService roleJurisdictionService;
    @Autowired
    private ModuleService moduleService;

    public void setModuleService(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public void setRoleJurisdictionService(RoleJurisdictionService roleJurisdictionService) {
        this.roleJurisdictionService = roleJurisdictionService;
    }

    /**
     * 进入角色列表
     * @param name
     * @param no
     * @param session
     * @param model
     * @return
     */
    @MyLog(value = "会员管理->角色管理->角色列表显示")
    @RequestMapping(value = "/toRoleList")
    public String toRoleList(@RequestParam(value = "name",required = false)String name,
                             @RequestParam(value = "no",required = false)String no, HttpSession session, Model model){
        model.addAttribute("name",name);
        int curr = 1;
        Page page = new Page();
        int count = roleService.count(name);
        page.setCount(count);
        if (no != null) {
            curr = Integer.parseInt(no);
            if (curr > page.getPageSum()) {
                curr = page.getPageSum();
            } else if (curr < 1) {
                curr = 1;
            }
        }
        page.setPageNo(curr);
        List<Role> roleList = roleService.roleListTiaoJian(name,(curr-1)*6);
        session.setAttribute("page", page);
        session.setAttribute("roleList", roleList);
        return "user/roleList";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/roleDelete")
    public String delete(@RequestParam("id")String id){
        int rs=roleService.delete(Integer.parseInt(id));
        if(rs>0){
            return "redirect:toRoleList";
        }else{
            return "redirect:toRoleList";
        }
    }

    /**
     * 增加角色
     * @param roleName
     * @return
     */
    @RequestMapping(value = "/roleAdd")
    public String roleAdd(@RequestParam("roleName")String roleName){
        int rs=roleService.add(roleName);
        if(rs>0){
            return "redirect:toRoleList";
        }else{
            return "redirect:toRoleList";
        }
    }

    /**
     * 根据roleid查询权限
     * @param rolrId
     * @return
     */
    @RequestMapping(value = "/RoleJurisdictionById")
    public Object RoleJurisdictionById(@RequestParam("roleId")String rolrId,HttpSession session){
        List<RoleJurisdiction>roleJurisdictionList=roleJurisdictionService.RoleJurisdictionById(Integer.parseInt(rolrId));
        Role role=roleService.xiangQing(Integer.parseInt(rolrId));
        session.setAttribute("roleJurisdictionList",roleJurisdictionList);
        session.setAttribute("role",role);
        return "user/roleJurisdiction";
    }

    /**
     * 判断角色名是否存在
     * @param roleName
     * @return
     */
    @RequestMapping(value = "/chackRoleName")
    @ResponseBody
    public String chackRoleName(@RequestParam("roleName")String roleName){
        Role role=roleService.chackName(roleName);
        Map<Object,Object> map=new HashMap<Object, Object>();
        if(role!=null){
            map.put("checking", "exists");
        }else{
            map.put("checking", "no");
        }
        return JSONArray.toJSONString(map);
    }

    /**
     * 功能删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/toRoleJurisdictionDelete")
    public String toRoleJurisdictionDelete(@Param("id")String id){
        int rs=roleJurisdictionService.delete(Integer.parseInt(id));
        if(rs>0){
            return "redirect:toRoleList";
        }else{
            return "redirect:toRoleList";
        }
    }

    /**
     * 跳转到增加角色
     * @param session
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/toRoleJurisdictionAdd")
    public String toAdminJurisdictionAdd(HttpSession session,@RequestParam("roleId")String roleId){
        List<Module> moduleList =moduleService.list();
        session.setAttribute("moduleList",moduleList);
        session.setAttribute("roleId",roleId);
        return "user/roleJurisdictionAdd";
    }


    /**
     * 增加操作
     * @param moduleId
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/RoleJurisdictionAdd")
    public String AdminJurisdictionAdd(@RequestParam("roleId")String roleId,@RequestParam("moduleId")String moduleId,
                                       Model model){
       int rs=roleJurisdictionService.chackModule(Integer.parseInt(roleId),Integer.parseInt(moduleId));
        if(rs>0){
            model.addAttribute("error","该模块此角色已拥有!");
            return "user/roleJurisdictionAdd";
        }else {
            int rs1 = roleJurisdictionService.add(Integer.parseInt(roleId),Integer.parseInt(moduleId));
            if (rs1 > 0) {
                return "redirect:toRoleList";
            } else {
                return "redirect:toRoleList";
            }
        }
    }
}
